package com.demo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.squareup.otto.Subscribe;

import com.vmcmonitor.common.CameraSurface;

import java.util.ArrayList;
import java.util.List;

import com.hcvsa.MyHCVSA;
import com.hcvsa.UserAcceptCall;
import com.hcvsa.UserHandUpCall;
import com.hcvsa.UserRejectCall;
import com.until.BusProvider;
import com.until.CItem;
import com.until.Until;
import com.view.MyVideoRel2;
import fun.flyee.pengshan.social.R;
import hcvs.hcvsa.MeetingEvent;

public class VideoActivity extends Activity implements OnClickListener {

    private Context context;

    private Button btnCameraFacing;

    private Spinner spinnerCodeRate;
    private Spinner spinnerDefinition;        //清晰度
    private Spinner spinnerDeflection;        //摄像头偏转角度

    private ArrayAdapter<CItem> codeRateAdapter;
    private ArrayAdapter<CItem> definitionAdapter;
    private ArrayAdapter<CItem> deflectionAdapter;

    private List<CItem> codeRateList;
    private List<CItem> definitionList;
    private List<CItem> deflectionList;

    private boolean isMyCall;        //是否为主动呼叫
    private int sessionid;        //用户回话ID，每个在线用户拥有唯一的sessionid
    private MyVideoRel2 myVideoRel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        BusProvider.getInstance().register(this);
        configInit();
        findView();
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        BusProvider.getInstance().unregister(this);
        if (myVideoRel != null) {
            try {
                myVideoRel.release();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        //关闭我的视频通道
        MyHCVSA.getInstance().CloseMyVideoChannel();
        super.onDestroy();
    }

    private void configInit() {
        context = this;
        Intent intent = this.getIntent();
        isMyCall = intent.getBooleanExtra("isMyCall", false);
        sessionid = intent.getIntExtra("sessionid", 0);

    }

    private void findView() {
        btnCameraFacing = (Button) findViewById(R.id.btnCameraFacing);
        spinnerCodeRate = (Spinner) findViewById(R.id.spinnerCodeRate);
        spinnerDefinition = (Spinner) findViewById(R.id.spinnerDefinition);
        spinnerDeflection = (Spinner) findViewById(R.id.spinnerDeflection);
        myVideoRel = findViewById(R.id.myVideoRel);
        btnCameraFacing.setOnClickListener(this);
    }

    private void init() {

//		spinnerInit();

        if (isMyCall) {//如果是我主动呼叫
            //发送音视频通话请求
            MyHCVSA.getInstance().getUserSessionMoudle().SendCallInviteToUser(sessionid, MeetingEvent.HCVS_CALL_TYPE_AV);
        } else {
            //接受呼叫
            MyHCVSA.getInstance().getUserSessionMoudle().OnMyAcceptCall(sessionid, MeetingEvent.HCVS_CALL_TYPE_AV);
        }

        myVideoRel.post(new Runnable() {

            @Override
            public void run() {
                //打开我的视频
                MyHCVSA.getInstance().OpenMyVideoChannel();
            }
        });

    }

    private void spinnerInit() {
        // 码率菜单初始化
        codeRateList = new ArrayList<CItem>();
        for (int i = 256; i < 4096; i += 256) {
            CItem cItem = new CItem(i, i + "kbps");
            codeRateList.add(cItem);
        }
        codeRateAdapter = new ArrayAdapter<CItem>(context, android.R.layout.simple_spinner_item, codeRateList);
        codeRateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerCodeRate.setAdapter(codeRateAdapter);
        Until.setSpinnerItemSelectedByKey(spinnerCodeRate, myVideoRel.getCameraSurface(0).getCameraHelper().getCurrentCodeRate());
        spinnerCodeRate.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                myVideoRel.getCameraSurface(0).getCameraHelper().setCurrentCodeRate(codeRateList.get(position).GetID());

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

        //清晰度菜单初始化
        definitionList = new ArrayList<CItem>();
        definitionList.add(new CItem(CameraSurface.VIDEO_DEFINITION_CIF, "极速"));
        definitionList.add(new CItem(CameraSurface.VIDEO_DEFINITION_D1, "标清"));
        definitionList.add(new CItem(CameraSurface.VIDEO_DEFINITION_720P, "高清"));
        definitionList.add(new CItem(CameraSurface.VIDEO_DEFINITION_1080P, "超清"));
        definitionAdapter = new ArrayAdapter<CItem>(context, android.R.layout.simple_spinner_item, definitionList);
        definitionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerDefinition.setAdapter(definitionAdapter);
        Until.setSpinnerItemSelectedByKey(spinnerDefinition, myVideoRel.getCameraSurface(0).getCameraHelper().getCurrentdefinition());
        spinnerDefinition.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                myVideoRel.getCameraSurface(0).getCameraHelper().setCurrentdefinition(definitionList.get(position).GetID());
                myVideoRel.getCameraSurface(0).StopCamera();
                myVideoRel.getCameraSurface(0).StartCamera();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

        //摄像头偏转
        deflectionList = new ArrayList<CItem>();
        for (int i = 0; i < 360; i += 90) {
            deflectionList.add(new CItem(i, i + "度"));
        }
        deflectionAdapter = new ArrayAdapter<CItem>(context, android.R.layout.simple_spinner_item, deflectionList);
        deflectionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerDeflection.setAdapter(deflectionAdapter);
        Until.setSpinnerItemSelectedByKey(spinnerDeflection, myVideoRel.getCameraSurface(0).getCameraHelper().getCurrentCameraDeflection());
        spinnerDeflection.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                myVideoRel.getCameraSurface(0).getCameraHelper().setCurrentCameraDeflection(deflectionList.get(position).GetID());
                myVideoRel.getCameraSurface(0).StopCamera();
                myVideoRel.getCameraSurface(0).StartCamera();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });
    }

    @Subscribe
    public void UserHandUpCall(UserHandUpCall data) {
        Toast.makeText(context, "用户挂断了与你的通话！", Toast.LENGTH_LONG).show();
        finish();
    }

    @Subscribe
    public void UserRejectCall(UserRejectCall data) {
        Toast.makeText(context, "用户拒绝了你的呼叫", Toast.LENGTH_LONG).show();
        finish();
    }

    @Subscribe
    public void UserAcceptCall(UserAcceptCall data) {
        Toast.makeText(context, "用户接受了你的呼叫", Toast.LENGTH_LONG).show();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //前后摄像头切换
            case R.id.btnCameraFacing:
                myVideoRel.getCameraSurface(0).cameraFacingChanged();
                break;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            //挂断
            MyHCVSA.getInstance().getUserSessionMoudle().OnMyCallHandUp(sessionid, MeetingEvent.HCVS_CALL_TYPE_AV);
            finish();
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }
}
